package com.example.gymmanagebackend.service.impl;
import com.example.gymmanagebackend.mapper.MembersMapper;
import com.example.gymmanagebackend.mapper.UserMapper;
import com.example.gymmanagebackend.pojo.Members;
import com.example.gymmanagebackend.pojo.User;
import com.example.gymmanagebackend.service.MembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MembersServiceImpl implements MembersService {

    @Autowired
    private MembersMapper membersMapper;

    @Autowired
    private UserMapper usersMapper;

    @Override
    public int addMember(Members member) {
        return membersMapper.addMember(member);
    }

    @Override
    public int deleteMemberByName(String name) {
        return membersMapper.deleteMemberByName(name);
    }

    @Override
    public int updateMember(Members member) {
        return membersMapper.updateMember(member);
    }

    @Override
    public List<Members> getAllMembers() {
        return membersMapper.getAllMembers();
    }

    @Override
    public Members findMemberById(Integer id) {
        return membersMapper.findMemberById(id);
    }

    @Override
    public Members findMemberByPhone(String phone) {
        return membersMapper.findMemberByPhone(phone);
    }

    @Override
    public Members findMemberByName(String name) {return membersMapper.findMemberByName(name);}

    @Override
    public Page<Members> findAll(Pageable pageable) {
        List<Members> members = membersMapper.findAll(pageable);
        long total = membersMapper.count();
        return new PageImpl<>(members, pageable, total);
    }
    @Override
    public String findPhoneByName(String name) {
        Members member = membersMapper.findMemberByName(name);
        return member != null ? member.getPhone() : null;
    }
    @Override
    @Transactional
    public void registerMember(Members member, String password) {
        membersMapper.addMember(member); // 插入Members表
        User user = new User();
        user.setUsername(member.getPhone());
        user.setPassword(password);
        user.setIdentify("mem"); // 设置身份为会员
        usersMapper.addUser(user); // 插入Users表
    }

    @Override
    public List<Members> getAgeDistribution() {
        return membersMapper.getAgeDistribution();
    }

    @Override
    public List<Members> getGenderDistribution() {
        return membersMapper.getGenderDistribution();
    }

    @Override
    public List<Members> getMemberGrowthTrend() {
        return membersMapper.getMemberGrowthTrend();
    }
    @Override
    public List<Members> getCardPurchaseStatistics() {
        return membersMapper.getCardPurchaseStatistics();
    }
    @Override
    public Page<Members> findBySearch(String search, Pageable pageable) {
        int offset = pageable.getPageNumber() * pageable.getPageSize();
        int limit = pageable.getPageSize();

        List<Members> members = membersMapper.findBySearch(search, offset, limit);
        long total = membersMapper.countBySearch(search);

        return new PageImpl<>(members, pageable, total);
    }
}